<template>
  <div class>
    <MyTable :arr="list">
      <template v-slot:header>
        <th>#</th>
        <th>商品名称</th>
        <th>价格</th>
        <th>标签</th>
        <th>操作</th>
      </template>

      <template v-slot:body="scope">
        <td>{{ scope.data.id }}</td>
        <td>{{ scope.data.goods_name }}</td>
        <td>{{ scope.data.goods_price }}</td>
        <td>
          <input
            class="tag-input form-control"
            v-model="scope.data.inputValue"
            style="width: 100px;"
            type="text"
            v-if="scope.data.inputVisible"
            v-focus
            @blur="scope.data.inputVisible = false"
            @keydown.enter="enterFn(scope.data)"
            @esc="scope.data.inputValue"
          />
          <button
            v-else
            style="display: block;"
            class="btn btn-primary btn-sm add-tag"
            @click="scope.data.inputVisible = true"
          >+Tag</button>
          <span
            v-for="(item,index) in scope.data.tags"
            class="badge badge-warning"
            :key="index"
          >{{item}}</span>
        </td>
        <td>
          <button class="label label-info" @click="removebtn(scope.data.id)">删除</button>
        </td>
      </template>

      <!-- <template v-slot="scope">{{scope.data.tags}}</template> -->
    </MyTable>
  </div>
</template>

<script>
import MyTable from "../src/components/MyTable.vue";
export default {
  data() {
    return {
      list: []
    };
  },
  created() {
    this.fn();
  },
  methods: {
    fn() {
      this.$axios({
        method: "GET",
        url: "/api/goods"
      }).then(res => (this.list = res.data.data));
    },
    removebtn(id) {
      const index = this.list.findIndex(item => item.id === id);
      this.list.splice(index, 1);
    },
    enterFn(obj) {
      // console.log(obj.inputValue);
      if (obj.inputValue.trim().length === 0) {
        return alert("请输入数据");
      }
      obj.tags.push(obj.inputValue); //将字符串添加到数组
      obj.inputValue = ""; //情况里面的数据
    }
  },
  components: { MyTable }
};
</script>

<style scoped>
</style>
